<template>
	<view class="pad20T pad50B clearfix overflow-x-hidden">
		<uni-steps active-color="#f83d38" :options="[{title: '保费试算'}, {title: '补充资料'}, {title: '身份验证'}]"
			:active="2"></uni-steps>
		<uni-forms :value="formData" ref="form" validate-trigger="bind" err-show-type="toast">
			<uni-section class="pub-title" title="会员身份验证" type="line"></uni-section>
			<uni-card class="box-shadow-10 card-pub">
				<uni-forms-item label="省市地区" name="shengsdq">
					<uni-easyinput type="text" v-model="formData.shengsdq" placeholder="" :inputBorder="true" disabled />
				</uni-forms-item>
				<uni-forms-item label="姓名" name="name">
					<uni-easyinput type="text" v-model="formData.name" placeholder="" :inputBorder="true" disabled />
				</uni-forms-item>
				<uni-forms-item label="身份证号" name="zhengjhm">
					<uni-easyinput type="text" v-model="formData.zhengjhm" placeholder="" :inputBorder="true" disabled />
				</uni-forms-item>
				<uni-forms-item label="手机号码" name="mobile">
					<uni-easyinput type="text" v-model="formData.mobile" placeholder="" :inputBorder="true" disabled />
				</uni-forms-item>
				<uni-forms-item label="验证码" name="yanzm" v-if="showYanzm">
					<uni-row>
						<uni-col :span="13">
							<uni-easyinput type="text" v-model="formData.yanzm" placeholder="请输入验证码" :inputBorder="true" />
						</uni-col> 
						<uni-col :span="11">
							<button class="fxr margin5T" type="warn" size="mini" @click="getYanzm()">重新获取</button>
						</uni-col>
					</uni-row>
					
				</uni-forms-item>
			</uni-card>
			
			<uni-section class="pub-title" title="人工验证照片" type="line" v-if="showRengsh"></uni-section>
			<uni-card :isShadow="true" v-if="showRengsh">
				<uni-forms-item label-width="90" label="身份证正面" required>
					<u-upload width="160" height="160" max-count="1" :action="action" :header="header"
						@on-success="onSuccess"></u-upload>
				</uni-forms-item>
				<uni-forms-item label-width="90" label="身份证反面" required>
					<u-upload width="160" height="160" max-count="1" :action="action" :header="header"
						@on-success="onSuccess2"></u-upload>
				</uni-forms-item>
				<uni-forms-item label-width="90" label="手持身份证" required>
					<u-upload width="160" height="160" max-count="1" :action="action" :header="header"
						@on-success="onSuccess3"></u-upload>
				</uni-forms-item>
			</uni-card>
			
		</uni-forms>
		
		<block v-if="showRengsh == true">
			<button @click="submitForm2" class="btn-red bottom-btn border-no">人工认证</button>
		</block>
		<block v-else>
			<button @click="submitForm" class="btn-red bottom-btn border-no">自动认证</button>
		</block>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showRengsh: false,
				shenfzzm: null,
				shenfzfm: null,
				shoucsfz: null,
				header: {
					"X-Access-Token": uni.getStorageSync("token")
				},
				action: getApp().globalData.serviceUrl + "/sys/common/upload",
				showYanzm: false,
				formData: {
					shengsdq: uni.getStorageSync("toubrxx").province + "/" + uni.getStorageSync("toubrxx").city + "/" + uni.getStorageSync("toubrxx").county,
					name: uni.getStorageSync("toubrxx").name,
					mobile: uni.getStorageSync("toubrxx").mobile,
					zhengjhm: uni.getStorageSync("toubrxx").zhengjhm,
				},
			}
		},
		
		onLoad() {
			this.getYanzm()
		},
		
		onUnload() {
			// wx.navigateTo({
			// 	url: '/pages/baofShis/baofShis'
			// })
		},
		
		methods: {
			onSuccess(data, index, lists, name) {
				if (data.success)
					this.shenfzzm = data.message
			},
			
			onSuccess2(data, index, lists, name) {
				if (data.success)
					this.shenfzfm = data.message
			},
			onSuccess3(data, index, lists, name) {
				if (data.success)
					this.shoucsfz = data.message
			},
			
			getYanzm() {
				let that = this
				uni.showLoading({
				    title: '身份认证中...'
				});
				uni.request({
					url: getApp().globalData.serviceUrl + "/electronic/requestSubmitInfo",
					data: {
						state: uni.getStorageSync("toubrxx").province,
						city: uni.getStorageSync("toubrxx").city,
						firstname: uni.getStorageSync("toubrxx").ming,
						lastname: uni.getStorageSync("toubrxx").xing,
						idcard: this.formData.zhengjhm,
						policyId: uni.getStorageSync("selectedOrder").kuybOrderPolicyList[0].id,
						mobile: this.formData.mobile
					},
					dataType: 'json',
					method: 'POST',
					header: {
						"X-Access-Token": uni.getStorageSync("token")
					},
					success: (resp) => {
						uni.hideLoading()
						if (resp.data.success) {
							if(resp.data.result.token) {
								uni.setStorageSync("shenfyz_token", resp.data.result.token)
								return;
							}
							
							if(resp.data.result.errorcode == -113) {
								uni.showModal({
								    title: '提示',
								    content: '上次提交的认证数据还在人工审核中',
									showCancel: false,
								    success: function (res) {
								        if (res.confirm) {
								            console.log('用户点击确定');
											uni.switchTab({
												url: '/pages/home/home'
											});
											
								        } else if (res.cancel) {
								            console.log('用户点击取消');
								        }
								    }
								});
								
								return
							}
							
							if(resp.data.result.errorcode == 0) {
								that.showYanzm = true
								uni.showToast({
									icon: "none",
									title: "验证码已发送到注册手机"
								})
								return
							}
							
							if(resp.data.result.errorcode == -108) {
								that.showYanzm = true
								uni.showToast({
									icon: "none",
									title: "验证码获取太频繁，稍后再试"
								})
								return
							}
							
							if(!resp.data.result.token)
								that.showYanzm = true
								
							
							
						} else {
							uni.showToast({
								icon: "none",
								title: resp.message
							})
						}
						
					}
				})
			},
			
			updateOrder() {
				let that = this
				let order = uni.getStorageSync("selectedOrder")
				
				// 行驶证照片
				order.kuybOrderPolicyList[0].xingsz = uni.getStorageSync("toubrxx").xingsz,
				// 仪表盘照片
				order.kuybOrderPolicyList[0].yibp= uni.getStorageSync("toubrxx").yibp
				// 被保险人地址
				order.kuybOrderPolicyList[0].beibxrdz= uni.getStorageSync("toubrxx").province + "/" + uni.getStorageSync("toubrxx").city + "/" + uni.getStorageSync("toubrxx").county + "/" + uni.getStorageSync("toubrxx").address
				// 使用性质
				order.kuybOrderPolicyList[0].shiyxz= uni.getStorageSync("carInfo").shiyxz
				// 被保险人姓名
				order.kuybOrderPolicyList[0].beibxr= uni.getStorageSync("toubrxx").name
				// 被保险人姓
				order.kuybOrderPolicyList[0].beibxrX= uni.getStorageSync("toubrxx").xing
				// 被保险人名
				order.kuybOrderPolicyList[0].beibxrM= uni.getStorageSync("toubrxx").ming
				// 被保险人联系电话
				order.kuybOrderPolicyList[0].beibxrlxdh= uni.getStorageSync("toubrxx").mobile
				// 被保险人身份证
				order.kuybOrderPolicyList[0].beibxrsfz= uni.getStorageSync("toubrxx").zhengjhm
				// 被保险人邮箱
				order.kuybOrderPolicyList[0].beibxryx= uni.getStorageSync("toubrxx").email
				// 车架号
				order.kuybOrderPolicyList[0].chejh= uni.getStorageSync("carInfo").vin
				// 发动机号
				order.kuybOrderPolicyList[0].fadjh= uni.getStorageSync("carInfo").fadjh
				// 车型id
				order.kuybOrderPolicyList[0].carid= uni.getStorageSync("carInfo").carid
				// 电子签章
				order.userToken = uni.getStorageSync("shenfyz_token")
				
				uni.showLoading({
				    title: '生成合同中...'
				})
				uni.request({
					url: getApp().globalData.serviceUrl + "/order/kuybOrder/edit",
					data: order,
					header: {
						"X-Access-Token": uni.getStorageSync("token")
					},
					method: 'PUT',
					dataType: 'json',
					success: (resp) => {
						uni.hideLoading()
						
						if (resp.data.success) {
							uni.redirectTo({
								url: '../../pages2/paymentSuccess/paymentSuccess'
							})
							
						} else {
							uni.showToast({
								title: resp.data.message,
								icon: 'none',
								duration: 3000
							});
						}
					},
					complete() {
						uni.hideLoading()
					}
				});
			},
			
			submitForm: function(form) {
				let that = this
				
				if(that.showYanzm && !that.formData.yanzm) {
					uni.showToast({
						title: '验证码必填!',
						icon: 'none'
					});
					return
				}
				
				uni.showLoading({
				    title: '提交数据中...'
				});
				uni.request({
					url: getApp().globalData.serviceUrl + "/electronic/requestValidateInfo",
					data: {
						mobile: uni.getStorageSync("toubrxx").mobile,
						code: that.formData.yanzm,
						policyId: uni.getStorageSync("selectedOrder").kuybOrderPolicyList[0].id,
					},
					dataType: 'json',
					method: 'POST',
					header: {
						"X-Access-Token": uni.getStorageSync("token")
					},
					success: (resp) => {
						uni.hideLoading()
						if (resp.data.success) {
							if(resp.data.result.token) {
								uni.setStorageSync("shenfyz_token", resp.data.result.token)
								// uni.redirectTo({
								// 	url: '../../pages2/payment/payment'
								// })
								that.updateOrder()
							}
							
							if(resp.data.result.errorcode == -111) {
								uni.showToast({
									icon: "none",
									title: "短信验证码错误"
								})
								return
							}
							
							if(resp.data.result.errorcode == -117) {
								that.showRengsh = true
								
								uni.showToast({
									icon: "none",
									title: "自动审核失败，上传指定照片后，人工审核。"
								})
								return
							}
							
						} else {
							uni.showToast({
								icon: none,
								title: resp.message
							})
						}
						
					}
				})
			},
			
			submitForm2: function(form) {
				let that = this
				
				if(!that.shenfzzm) {
					uni.showToast({
						icon: "none",
						title: "身份证正面必填"
					})
					return
				}
				
				if(!that.shenfzfm) {
					uni.showToast({
						icon: "none",
						title: "身份证反面必填"
					})
					return
				}
				
				if(!that.shoucsfz) {
					uni.showToast({
						icon: "none",
						title: "手持身份证必填"
					})
					return
				}
				
				uni.request({
					url: getApp().globalData.serviceUrl + "/electronic/requestUploadPic",
					data: {
						policyId: uni.getStorageSync("order").kuybOrderPolicyList[0].id,
						mobile: uni.getStorageSync("toubrxx").mobile,
						pic1: that.shenfzzm,
						pic2: that.shenfzfm,
						pic3: that.shoucsfz,
					},
					dataType: 'json',
					method: 'POST',
					header: {
						"X-Access-Token": uni.getStorageSync("token")
					},
					success: (resp) => {
						if (resp.data.success) {
							if(resp.data.result.errorcode == -113) {
								uni.showToast({
									icon: "none",
									title: "上次提交的认证数据还在人工审核中"
								})
								return
							}
							
							if(resp.data.result.errorcode == 0) {
								uni.navigateTo({
									url: '../../pages2/shougyzts/shougyzts'
								})
							}
							
						} else {
							uni.showToast({
								icon: "none",
								title: resp.data.message
							})
						}
						
					}
				})
				
				
			}
		}
	}
</script>

<style>

</style>
